﻿using System;
using System.Data;
using Besture.Common;

namespace Besture.UI.Admin.Control
{
    public partial class UploadMultiPhoto : System.Web.UI.UserControl
    {
        /// <summary>
        /// DIV ID
        /// </summary>
        public string PhotoDivID = System.Guid.NewGuid().ToString();
        /// <summary>
        /// 上传图片路径
        /// </summary>
        public string FolderPath { get; set; }
        /// <summary>
        /// 是否需要生成缩略图
        /// </summary>
        public bool NeedThumbnail { get; set; }
        /// <summary>
        /// 缩略图路径
        /// </summary>
        public string ThumbnailFolderPath { get; set; }
        /// <summary>
        /// 缩略图宽度
        /// </summary>
        public int ThumbnailWidth { get; set; }
        /// <summary>
        /// 缩略图高度
        /// </summary>
        public int ThumbnailHeight { get; set; }
        /// <summary>
        /// 是否需要生成第三张图片
        /// </summary>
        public bool NeedThumbnailThree { get; set; }
        /// <summary>
        /// 第三张缩略图路径
        /// </summary>
        public string ThumbnailFolderPathThree { get; set; }
        /// <summary>
        /// 第三张缩略图宽度
        /// </summary>
        public int ThumbnailWidthThree { get; set; }
        /// <summary>
        /// 第三张缩略图高度
        /// </summary>
        public int ThumbnailHeightThree { get; set; }
        /// <summary>
        /// 是否必须上传
        /// </summary>
        public bool Required { get; set; }
        /// <summary>
        /// 必须上传图片
        /// </summary>
        public string RequiredImageUrl = "/Admin/Themes/Images/dispensable.png";
        /// <summary>
        /// 提示文字
        /// </summary>
        public string Tips { get; set; }
        /// <summary>
        /// 上传图片张数
        /// </summary>
        public int Count { get; set; }

        public string ImgIdArgs { get { return txtImgIdArgs.Text; } set { txtImgIdArgs.Text = ImgIdArgs; } }

        public string ImgUrlArgs { get { return txtImgUrlArgs.Text; } set { txtImgUrlArgs.Text = ImgUrlArgs; } }

        public string ImgRemarkArgs { get { return txtImgRemarkArgs.Text; } set { txtImgRemarkArgs.Text = ImgRemarkArgs; } }

        public string AddImgUrlArgs { get { return txtAddImgUrlArgs.Text; } }

        public string AddImgRemarParamIDkArgs { get { return txtAddImgRemarParamIDkArgs.Text; } }

        public string DelIdArgs { get { return txtDelIdArgs.Text; } }

        public string ImgId
        {
            get { return txtImgID.Text; }
        }

        /// <summary>
        /// 初始化图片
        /// </summary>
        public DataTable InitPhotoTable = null;

        public string InitText = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            //必须上传
            if (Required)
            {
                txtAllImage.CssClass = "imageRequired";
                RequiredImageUrl = "/Admin/Themes/Images/indispensable.gif";
            }

            //上传文件夹缺省情况
            if (string.IsNullOrEmpty(FolderPath))
            {
                FolderPath = Constants.PATH_IMAGE_CONTENT;
            }
            
            //上传提示文字
            if (Tips == "")
            {
                Tips = "请上传图片";
            }

            if(InitPhotoTable != null)
            {
                InitText = InitPhotoDiv(InitPhotoTable);
                hfCount.Value = InitPhotoTable.Rows.Count.ToString();
            }
        }

        protected string InitPhotoDiv(DataTable table)
        {
            string text = string.Empty;
            string txtImgIdArgsValue = string.Empty;
            string txtImgUrlArgsValue = string.Empty;
            string txtImgRemarkArgsValue = string.Empty;
            string txtImgId = string.Empty;

            //清空(防止回发没清空)
            txtAllImage.Text = "";
            foreach (DataRow row in table.Rows)
            {
                string timestamp = row["ID"].ToString();
                string imgUrl = row["ImageUrl"].ToString();
                string imageRemark = row["ImageRemark"].ToString();

                text = text + ("<div class='element' id='" + timestamp + "'>");
                text = text + ("    <div>");
                text = text + ("         <img src='" + FolderPath + imgUrl + "' alt='' />");
                text = text + ("    </div>");
                text = text + ("    <p style='margin: 0em 0;'>");
                //text = text + ("        <input ID='Input_" + timestamp + "' Name='Input_" + timestamp + "' type='text' value='"+imageRemark+"' />");
                text = text + ("    </p>");
                text = text + ("    <p  class='buttonP'>");
                text = text + ("        <img src='/Admin/Themes/Images/del.gif' alt='删除图片' class='delPhoto' /><a href=\"javascript:DelPhoto('" + timestamp + "')\">删除</a>");
                text = text + ("    </p>");
                text = text + ("</div>");

                txtImgId += timestamp + ",";
                txtImgIdArgsValue += timestamp +",";
                txtImgUrlArgsValue += imgUrl + ",";
                txtImgRemarkArgsValue += "Input_"+timestamp + ",";
                txtAllImage.Text += "i";
            }
            txtImgIdArgs.Text = txtImgIdArgsValue;
            txtImgUrlArgs.Text = txtImgUrlArgsValue;
            txtImgRemarkArgs.Text = txtImgRemarkArgsValue;
            txtImgID.Text = txtImgId;
            return text;
        }
    }
}